The language construct empty() appears rather versatile. It’s like a Swiss army knife with a thousand blades, ready to hurt you if you grab it by the wrong end. Or a jack of all trades, master of none. Most of all, empty() is a poor communicator.
I spot empty() in poorly-aged closed-source projects. I discover empty() in brand-new empty() again in open-source projects with millions of downloads. So what is the problem with empty() when so many people use it?Determine whether a variable is considered to be empty. A variable is considered empty if it does not exist or if its value equals false. empty() does not generate a warning if the variable does not exist.
If you ignore the internal implementation and possible performance issues, using empty() is identical to using isset() and a Losse Comparision withfalse.
You include a file that you expect to declare a variable. The file may not exist or may not declare the variable.
Instead of using empty() to verify that an included file exists and declares a variable, set the variable to a suitable default value, include the file when it exists, and verify that the variable has an acceptable type and value.
💡 Avoid writing code that relies on including files that declare variables or return values.
value)); // 👋(bool)true
console.log( 'Code is Poetry' );
value)); // 👋(bool)true
var_dump(empty($object->otherValue)); // 👋(bool)true
value)) {
+ if ($this->value === null) {
// ...
}
+ // handle other possible types and values
+
// ...
}
}
value)) {
+ if ($this->value === null) {
// ...
}
// ...
}
}
💡 Avoid writing classes with instance or static properties that accept multiple types. Add property type declarations or DocBlocks to document property types.
💡 Avoid writing functions or methods with parameters that accept multiple types. Add parameter type declarations or DocBlocks to document function and method parameter types.
bar()) {
+if ($foo->bar() === null) {
// ...
}
+// handle other possible types and values
bar()) {
+if ($foo->bar() === null) {
// ...
}
💡 Avoid writing functions or methods that return values of multiple types. Add return type declarations or DocBlocks to document function and method return types.
?php
declare(strict_types=1);
final class Foo
{
private $value;
public function bar()
{
- if (empty($this->value)) {
+ if ($this->value === []) {
// ...
}
+ // handle other possible types and values
+
// ...
}
}
value)) {
+ if ($this->value === []) {
// ...
}
// ...
}
}
💡 Avoid writing classes with instance or static properties that accept multiple types. Add property type declarations or DocBlocks to document property types.
bar()) {
+if ($foo->bar() === []) {
// ...
}
+// handle other possible types and values
bar()) {
+if ($foo->bar() === []) {
// ...
}
You have a class with an instance or a static property and currently use empty() to verify the property value.
Instead of using empty() to verify the property value when the property can assume multiple types, compare the instance or static property with false and handle each possible case separately.
value)) {
+ if ($this->value === false) {
// ...
}
+ // handle other possible types and values
+
// ...
}
}
value)) {
+ if (!$this->value) {
// ...
}
// ...
}
}
💡 Avoid writing classes with instance or static properties that accept multiple types. Add property type declarations or DocBlocks to document property types.
💡 Avoid writing functions or methods with parameters that accept multiple types. Add parameter type declarations or DocBlocks to document function and method parameter types.
bar()) {
+if ($foo->bar() === false) {
// ...
}
+// handle other possible types and values
bar()) {
+if (!$foo->bar()) {
// ...
}
💡 Avoid writing functions or methods that return values of multiple types. Add return type declarations or DocBlocks to document function and method return types.
value)) {
+ if ($this->value === 0.0) {
// ...
}
+ // handle other possible types and values
+
// ...
}
}
value)) {
+ if ($this->value === 0.0) {
// ...
}
// ...
}
}
💡 Avoid writing classes with instance or static properties that accept multiple types. Add property type declarations or DocBlocks to document property types.
💡 Avoid writing functions or methods with parameters that accept multiple types. Add parameter type declarations or DocBlocks to document function and method parameter types.
bar()) {
+if ($foo->bar() === 0.0) {
// ...
}
+// handle other possible types and values
bar()) {
+if ($foo->bar() === 0.0) {
// ...
}
value)) {
+ if ($this->value === 0) {
// ...
}
+ // handle other possible types and values
+
// ...
}
}
value)) {
+ if ($this->value === 0) {
// ...
}
// ...
}
}
💡 Avoid writing classes with instance or static properties that accept multiple types. Add property type declarations or DocBlocks to document property types.
You have a function or a method with a parameter that could be an int with the value 0.
Instead of using empty() to verify the parameter value when the parameter can assume multiple types, compare the parameter with 0 and handle each possible case separately.
💡 Avoid writing functions or methods with parameters that accept multiple types. Add parameter type declarations or DocBlocks to document function and method parameter types.
bar()) {
+if ($foo->bar() === 0) {
// ...
}
+// handle other possible types and values
bar()) {
+if ($foo->bar() === 0) {
// ...
}
You have a class with an instance or a static property and currently use empty() to verify the property value.
Instead of using empty() to verify the property value when the property can assume multiple types, compare the instance or static property with ‘ ‘ (or ‘0’) and handle each possible case separately.
value)) {
+ if ($this->value === '') {
// ...
}
+ // handle other possible types and values
+
// ...
}
}
value)) {
+ if ($this->value === '') {
// ...
}
// ...
}
}
💡 Avoid writing classes with instance or static properties that accept multiple types. Add property type declarations or DocBlocks to document property types.
You have a function or a method with a parameter that could be a string with the value ” (or '0').
💡 Avoid writing functions or methods with parameters that accept multiple types. Add parameter type declarations or DocBlocks to document function and method parameter types.
You have a function or a method with a return value that could be a string with the value ” (or ‘0’).
Instead of using empty() to verify the return value at the call site when the return value can assume multiple types, compare the return value with ‘ ‘ (or ‘0’) and handle each possible case separately.
bar()) {
+if ($foo->bar() === '') {
// ...
}
+// handle other possible types and values
bar()) {
+if ($foo->bar() === '') {
// ...
}
');
var_dump(empty($value)); // (bool)true
© 2026 Cloudazia Systems. Created with ❤ using WordPress and Kubio